
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLoad: false,
        sku: [{
            name: '瓶数',
            value: [
                {spe: "1瓶", is_show: 0},
                {spe: "2瓶", is_show: 0},
                {spe: "6瓶", is_show: 0},
                {spe: "12瓶", is_show: 0}
            ]
        },
        {
            name: "口味",
            value: [
                {spe: "浓香", is_show: 0},
                {spe: "酱香", is_show: 0}
            ]
        }],
        sku_list: {
            '0_0|1_0': {original_price: "20", price: "10", weight: "1", stock: "10000", cost_price: "5", show: "0"},
            '0_0|1_1': {original_price: "20", price: "15", weight: "1", stock: "10", cost_price: "10", show: "1"},
            '0_1|1_0': {original_price: "40", price: "20", weight: "2", stock: "10000", cost_price: "10", show: "1"},
            '0_1|1_1': {original_price: "35", price: "30", weight: "2", stock: "10", cost_price: "25", show: "0"},
            '0_2|1_0': {original_price: "120", price: "58", weight: "6", stock: "10000", cost_price: "30", show: "1"},
            '0_2|1_1': {original_price: "100", price: "75", weight: "6", stock: "5", cost_price: "60", show: "0"},
            '0_3|1_0': {original_price: "240", price: "109", weight: "12", stock: "10000", cost_price: "60", show: "1"},
            '0_3|1_1': {original_price: "280", price: "140", weight: "12", stock: "2", cost_price: "120", show: "1"}
        },
        skuIdx:[],
        disArr: []
    },

    state: {
        spliter: '\u2299'
    },
    res: {},

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let arr = [];
        const sku_list = this.data.sku_list;
        for(let key in sku_list) {
            if (sku_list[key].show == 0) {
                arr.push(key);
            }
        }
        if (arr.length > 0) {
            for (let i = 0; i < arr.length; i++) {
                let curr = arr[i];
                const values = curr.split('|');
                const allSets = this.powerset(values);
                for (let j = 0; j < allSets.length; j++) {
                    let set = allSets[j];
                    let key = set.join('|');
                    if (this.res[key]) {
                        this.res[key].skus.push(true)
                    } else {
                        this.res[key] = {
                            skus: [true]
                        }
                    }
                }
            }
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        let arr = [];
        let disArr = [];
        for(let i = 0; i < this.data.sku.length; i++) {
            arr.push(-1);
            disArr.push([]);
        }
        this.setData({
            isLoad: true,
            skuIdx: arr,
            disArr
        })

        if (this.data.sku.length === 0) {
            this.updateStatus(this.getSelectedItem());
        }
    },

    // 点击规格事件
    handleSpe(e) {
        const dataset = e.currentTarget.dataset;
        const disArr = this.data.disArr;
        if (disArr[dataset.index].indexOf(dataset.idx) >= 0) return;
        let skuIdx = this.data.skuIdx;
        if (skuIdx[dataset.index] == dataset.idx) {
            skuIdx[dataset.index] = -1;
        } else {
            skuIdx[dataset.index] = dataset.idx;
        }
        
        this.setData({
            skuIdx
        })
        this.updateStatus(this.getSelectedItem());
    },

    // 获取选中的规格
    getSelectedItem() {
        let result = [];
        for(let i = 0; i < this.data.skuIdx.length;i++) {
            let idxItem = this.data.skuIdx[i];
            if (idxItem >= 0) {
                result.push(i+'_'+idxItem);
            } else {
                result.push('');
            }
        }
        return result;
    },

    // 正则替换多余符号
    trimSpliter(str, spliter) {
        // ⊙abc⊙ => abc
        // ⊙a⊙⊙b⊙c⊙ => a⊙b⊙c
        var reLeft        = new RegExp('^' + spliter + '+', 'g');
        var reRight       = new RegExp(spliter + '+$', 'g');
        var reSpliter = new RegExp(spliter + '+', 'g');

        return str.replace(reLeft, '')
            .replace(reRight, '')
            .replace(reSpliter, spliter)
    },

    // 更新规格可选状态
    updateStatus(selected) {
        let keys = this.data.sku;
        let disArr = this.data.disArr;
        for (let i = 0; i < keys.length; i++) {
            const data = keys[i].value;
            let copy = selected.slice();
            disArr[i] = [];
            for (let j = 0; j < data.length; j++) {
                const item = i+'_'+j;
                if (selected[i] == item) continue;
                copy[i] = item;
                let curr = this.trimSpliter(copy.join('|'), this.state.spliter);
                // curr = curr.split(this.state.spliter).join('|');
                if (this.res[curr]) {
                    disArr[i].push(j);
                } 
            }
        }
        this.setData({
            disArr
        })
    },

    // 取得集合的所有子集
    powerset(arr) {
        var ps = [[]];
        for (var i=0; i < arr.length; i++) {
            for (var j = 0, len = ps.length; j < len; j++) {
                ps.push(ps[j].concat(arr[i]));
            }
        }
        return ps;
    }
})